<?php

/**
 * image actions.
 *
 * @package    berta-s
 * @subpackage image
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class imageActions extends sfActions {
	private function initImage(){
		$this->image = $this->getRoute()->getObject();
		$this->forward404If(empty($this->image), "Image doesnt exist");
	}

	/* @TODO: DRY up. duplicate with section/entry! */
	/* @TODO: remove incoming data container ("entry_image") */
	private function saveImageFromRequest(sfWebRequest $request){
		if ($request->hasParameter("entry_image") || $request->getFiles("entry_image")!="") {
			$form=new EntryImageFormForBackend($this->image);
			$inputParams=$request->getParameter("entry_image");
			// have no idea, why we need this, Object should be initialized from form contructor
			if (!empty($this->image)){
				$inputParams["id"]=$this->image->getId();
			}

			$useFields=array();
			if (is_array($inputParams)){
				foreach ($inputParams as $name => $param){
					$useFields[]=$name;
				}
			}
			if (is_array($request->getFiles("entry_image"))){
				foreach ($request->getFiles("entry_image") as $name => $param){
					$useFields[]=$name;
				}
			}

			if (sizeof($useFields)>0){
				$form->useFields($useFields);
			}

			$form->bind($inputParams, $request->getFiles("entry_image"));
			if ($form->isValid()) {
				$form->save();
				$values=array();
				$values["id"]=$form->getObject()->getId();
				foreach ($inputParams as $key => $value){
					if ($key!="id" && $key!="_csrf_token"){
						$values[$key]=$form->getValue($key);
					}
				}
				foreach ($request->getFiles("entry_image") as $key => $value){
					if ($form->getValue($key) instanceOf sfValidatedFile){
						$values[$key]=PathHelper::getPublicPath($form->getValue($key)->getSavedName());
					}
				}
				$this->values=$values;

				$this->setTemplate("XMLHttp", "global");
			}else{
				$this->setTemplate("XMLHttpError", "global");
			}
			$this->form=$form;
		}else{
			$this->setTemplate("XMLHttp");
		}
	}


	public function executeCreate(sfWebRequest $request){
		$this->saveImageFromRequest($request);
	}

	public function executeUpdate(sfWebRequest $request){
		$this->initImage();
		$this->saveImageFromRequest($request);
	}

	public function executeShow(sfWebRequest $request){
		$this->initImage();
		$this->width=$this->height=null;
		if ($request->hasParameter("type")){
			$values=explode("x", $request->getParameter("type"));

			$this->width=$values[0];
			if (!empty($values[1])){
				$this->height=$values[1];
			}
		}
	}

	public function executePromote(sfWebRequest $request){
		$this->initImage();
		$this->image->promote();
		$this->setTemplate("XMLHttp", "global");
	}

	public function executeDemote(sfWebRequest $request){
		$this->initImage();
		$this->image->demote();
		$this->setTemplate("XMLHttp", "global");
	}

    public function executePosition(sfWebRequest $request){
		$this->initImage();
        $this->image->moveToPosition($request->getParameter("position"));
        $this->setTemplate("XMLHttp", "global");
    }

    public function executePositionBefore(sfWebRequest $request){
		$this->initImage();

        $beforeObject=Doctrine::getTable("EntryImage")->find($request->getParameter("before_object_id"));
        $beforePosition=$beforeObject->getPosition();
        $newPosition=$beforePosition-1;

        if ($newPosition>0){
            $this->image->moveToPosition($newPosition);
        }else{
            $this->image->moveToFirst();
        }
        $this->setTemplate("XMLHttp", "global");
    }

    public function executePositionAfter(sfWebRequest $request){
        $this->initImage();

        $afterObject=Doctrine::getTable("EntryImage")->find($request->getParameter("after_object_id"));
        $this->image->moveToPosition($afterObject->getPosition()+1);

        $this->setTemplate("XMLHttp", "global");
    }


	public function executeDelete(sfWebRequest $request){
		$this->initImage();
		$this->image->delete();
		$this->setTemplate("XMLHttp", "global");
	}




}
